package xgenerator.jdbc;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Vector;

public class TransactionFactory {
	
	public static Transaction getTransaction(Object transactionSource) {
		if(transactionSource instanceof Connection) {
			Connection conn = (Connection) transactionSource;
			try {
				conn.setAutoCommit(false);
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return new JdbcTransaction(conn);
		} else if(transactionSource instanceof Vector) {
			Vector<Connection> connVector = (Vector<Connection>) transactionSource;
			try {
				for(int i=0; i<connVector.size(); i++) {
					connVector.get(i).setAutoCommit(false);
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			return new MultiDataSourceTransaction(connVector);
		}
		
		return null;
	}

}
